unit MLPAAssaySelect;

interface

uses Windows, SysUtils, Classes, Variants, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, Grids, ADODB, DBGrids, DB;

type
  TdlgSelectAssay = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    dbgAssays: TDBGrid;
    Label1: TLabel;
    btnAddAssay: TButton;
    Label2: TLabel;
    Label3: TLabel;
    lblSampleInfo: TLabel;
    lblFilename: TLabel;
    procedure btnAddAssayClick(Sender: TObject);
    procedure dbgAssaysTitleClick(Column: TColumn);
  private
    { Private declarations }
    fkitid: string;
    frunid: integer;
    procedure SetCriteria(kitid:string;runid: integer; StartDate,EndDate: TDate);
  public
    { Public declarations }
    function SelectAssay(kitid:string;runid:integer;SampleInfo,Filename:string):integer;
    function CreateAssay(kitid:string;runid:integer;SampleInfo,Filename,WellData:string):integer;
  end;

var
  dlgSelectAssay: TdlgSelectAssay;

implementation

uses MLPAdm, MLPAAddAssay;

{$R *.DFM}
// HAVING (((tbl_runs.GelfileName)="Eddy run 1") AND ((tbl_runs.Date)=#7/27/2004#) AND ((tbl_assays.kitid)=5));

{ TdlgSelectAssay }

function TdlgSelectAssay.CreateAssay(kitid: string; runid: integer; SampleInfo,
  Filename,WellData: string): integer;
var
  samp_id:integer;
begin
  // locate the sample, and add if not exists
  If DM.tblSamples.State=dsEdit then
    DM.tblSamples.Post;
  if not DM.tblSamples.Locate('LabId',SampleInfo,[]) then
    DM.tblSamples.InsertRecord([nil,SampleInfo]);
  DM.tblAssays.InsertRecord([nil,DM.tblSamples.FieldValues['sampleid'],RunId,kitid,
                                     SampleInfo,WellData,FileName]);
  result:=DM.tblAssays.FieldValues['assayid'];
end;

procedure TdlgSelectAssay.dbgAssaysTitleClick(Column: TColumn);
{$J+}
 const PreviousColumnIndex : integer = -1;
{$J-}
begin
  if dbgAssays.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(dbgAssays.DataSource.DataSet) do
  begin
    if (0<=PreviousColumnIndex) and (PreviousColumnIndex<dbgAssays.Columns.Count) then
    begin
      dbgAssays.Columns[PreviousColumnIndex].title.Font.Style :=
      dbgAssays.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
    end;

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

    if (Pos(Column.Field.FieldName, string(Sort)) = 1)
    and (Pos(' DESC', string(Sort))= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

function TdlgSelectAssay.SelectAssay(kitid:string; runid: integer;SampleInfo,Filename:string): integer;
begin
  dbgAssays.Options:=dbgAssays.Options + [dgMultiSelect];
  SetCriteria(kitid,runid,-1,-1);
  lblSampleInfo.Caption:=SampleInfo;
  lblFilename.Caption:=Filename;
  DM.qrySelectAssay.Locate('SampleIDField;TraceFileName',VarArrayOf([SampleInfo,FileName]),[]);
  if ShowModal = mrOK then
  begin
    result:=DM.qrySelectAssay.FieldValues['assayid'];
  end
  else
    result:=-1;
end;

procedure TdlgSelectAssay.SetCriteria(kitid:string; runid: integer; StartDate,
  EndDate: TDate);
var
  sqlcriterium : string;
  runidcrit:string;
  kitidcrit:string;
begin
  if DM.qrySelectAssay.Active then
    DM.qrySelectAssay.Close;
  fkitid:=kitid;
  frunid:=runid;
  sqlcriterium:='';
  if (kitid='') and (runid=0) and (StartDate<0) and (EndDate<0) then
  begin
    DM.qrySelectAssay.SQL[3]:=';';
  end
  else begin
    if runid>0 then
      runidcrit:='((tbl_runs.runid)='+IntToStr(runid)+')'
    else
      runidcrit:='';
    if kitid<>'' then
      kitidcrit:='((tbl_assays.kitid)='+QuotedStr(kitid)+')'
    else
      kitidcrit:='';
    if (runidcrit='') or (kitidcrit='') then
      sqlcriterium:=runidcrit+kitidcrit
    else
      sqlcriterium:=runidcrit+' AND '+kitidcrit;
    if sqlcriterium<>'' then
      sqlcriterium:='HAVING ('+sqlcriterium+')';
    DM.qrySelectAssay.SQL[3]:=sqlcriterium+';';
  end;
  DM.qrySelectAssay.Open;
end;

procedure TdlgSelectAssay.btnAddAssayClick(Sender: TObject);
var
  assayid:integer;
begin
  assayid:=dlgAddAssay.AddAssay(fkitid,abs(frunid),lblSampleInfo.Caption);
  DM.qrySelectAssay.Requery;
  DM.qrySelectAssay.Locate('assayid',assayid,[]);
end;

end.
